import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    itemShowList:{
      xm:false,
      sjh:false,
      sfz:false,
      xb:false,
      nl:false,
    },
    mask:false,
    sexPop:false,
    agePop:false,
    activityid:'',
    ageList:[
      {
        id:1,
        text:'14岁以下'
      },
      {
        id:2,
        text:'14-28岁'
      },
      {
        id:3,
        text:'28-35岁'
      },
      {
        id:4,
        text:'35-45岁'
      },
    ],
    submitData:{
      activityid:'',
      name:'',//用户名
      contacttel:'',//电话
      sfz:'',//身份证
      gender:'',//性别
      gendertext:'',//性别文字
      age:'',//年龄
      agetext:'',//年龄文字
      humor:'',//心情
      note:'',//简介
    }

  },
  //性别/年龄选择
  showMask(e){
    let type = e.currentTarget.dataset.type
    if(type == "sex"){
      this.setData({
        mask:true,
        sexPop:true
      })
    }else if(type == "age"){
      this.setData({
        mask:true,
        agePop:true
      })
    }
  },
  selected(e){
    let type = e.currentTarget.dataset.type
    let text = e.currentTarget.dataset.text
    let val = e.currentTarget.dataset.val
    if(type == 'sex'){
      this.data.submitData.gendertext = text
      this.data.submitData.gender = val
      this.setData({
        mask:false,
        sexPop:false,
        submitData:this.data.submitData
      })
    }else if(type == 'age'){
      this.data.submitData.agetext = text
      this.data.submitData.age = val
      this.setData({
        mask:false,
        agePop:false,
        submitData:this.data.submitData
      })
    }
  },
  //输入框
  changeInput(e){
    let valkey = e.currentTarget.dataset.valkey
    let val = e.detail
    this.data.submitData[valkey] = val
    this.setData({ 
      submitData:this.data.submitData
    })

  },
  //提交
  async submit(){
    if(this.data.itemShowList.xm && (this.data.submitData.name == '' || this.data.submitData.name == null)){Toast('请输入用户名'); return}
    if(this.data.itemShowList.sjh && (this.data.submitData.contacttel == '' || this.data.submitData.contacttel == null)){Toast('请输入电话号码'); return}
    if(this.data.itemShowList.sfz && (this.data.submitData.sfz == '' || this.data.submitData.sfz == null)){Toast('请输入身份证号'); return}
    if(this.data.itemShowList.xb && (this.data.submitData.gendertext == '' || this.data.submitData.gendertext == null)){Toast('请选择性别'); return}
    if(this.data.itemShowList.nl && (this.data.submitData.age == '' || this.data.submitData.age == null)){Toast('请选择年龄范围'); return}
  
    let data= this.data.submitData
    data['flag'] = 'activity_apply'
    data['activityid'] = this.data.activityid
    console.log(this.data.submitData)
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      //返回上一级关闭当前页面
      Toast.success('提交成功')
      wx.navigateBack({
        delta: 1
      })
    }else {
      Toast(message)
    }  
  },
  //获取报名信息
  async getApply(){
    let data= {}
    data['flag'] = 'activity_applyup'
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      let data = result
      data.gendertext = result.gender
      data.gender = result.gender == "男" ? 1 : 0;
      data.contacttel = result.tel
      this.setData({
        submitData:data
      })
    }else {
      // Toast(message)
    }  
  },
  showInputItem(bmandshstr){
    let str = bmandshstr
    str.indexOf("xm") != -1 ? this.data.itemShowList.xm = true : this.data.itemShowList.xm = false;
    str.indexOf("sjh") != -1 ? this.data.itemShowList.sjh = true : this.data.itemShowList.sjh = false;
    str.indexOf("sfz") != -1 ? this.data.itemShowList.sfz = true : this.data.itemShowList.sfz = false;
    str.indexOf("xb") != -1 ? this.data.itemShowList.xb = true : this.data.itemShowList.xb = false;
    str.indexOf("nl") != -1 ? this.data.itemShowList.nl = true : this.data.itemShowList.nl = false;

    this.setData({
      itemShowList:this.data.itemShowList
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.showInputItem(options.bmandshstr)
    console.log(options)
    this.getApply()
    this.data.submitData.activityid = options.id
    this.setData({
      submitData:this.data.submitData,
      activityid:options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})